package com.smithcarlson.knucleotide
import java.io.File
import java.io.FileReader
import java.io.InputStream
import java.io.BufferedReader
import scala.collection.mutable.MutableList

object KNucleotide1 extends Application {
  override def main(args: Array[String]): Unit = {
    val startTimeMillis = System.currentTimeMillis()
    var file = new File("knucleotide-input-small1.txt")
    var r = new BufferedReader(new FileReader(file))
    val src = new SequenceFilteringLineSource(r, "THREE")
    while (src.hasMore()) {
      src.next()
    }
    r.close()
    val endTimeMillis = System.currentTimeMillis()
    println(endTimeMillis - startTimeMillis)
  }
}

trait TEnum[A] {
  trait Value { self: A => _values :+= this }
  private var _values = List.empty[A]
  def values = _values
}

trait NucleotideTreeNode {
  object Symbol extends TEnum[Symbol]
  sealed trait Symbol extends Symbol.Value
  case object A extends Symbol
  case object C extends Symbol
  case object G extends Symbol
  case object T extends Symbol
}

class SequenceFilteringLineSource(val in: BufferedReader, val sequenceId: String) {
  object State extends TEnum[State]
  sealed trait State extends State.Value
  case object NOT_TO_SEQ extends State
  case object IN_SEQ extends State

  var line: String = null
  var state: State = NOT_TO_SEQ
  val sequencePrefix = ">" + sequenceId

  def hasMore(): Boolean = {
    if (line != null) return true
    else {
      if (state == NOT_TO_SEQ) {
        do {
          line = in.readLine()
        } while (line != null && !line.startsWith(sequencePrefix))
        state = IN_SEQ
      }
      line = in.readLine()
      return line != null && !line.startsWith(">")
    }
  }
  def next(): String = { val _line = line; line = null; _line }
}